//
//  RightButtonList.m
//  RightSlide
//
//  Created by Pill Gong on 13-12-12.
//  Copyright (c) 2013年 Pill Gong. All rights reserved.
//

#import "RightButtonList.h"

#define IsiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)
#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)

@interface RightButtonList () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) CGFloat contentWidth;
@property (nonatomic, assign) BOOL isMoving;

@property (nonatomic, retain) UITableView *table;

@end

@implementation RightButtonList

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //self.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.8];
        [self setDefaultValues];
        [self addTableView];
        self.hidden = YES;
    }
    return self;
}

- (id)initWithContentWidth:(CGFloat)contentWidth {

    self.contentWidth = contentWidth;
    self = [self initWithFrame:CGRectMake(ScreenWidth, 0, _contentWidth, 0)];
    return self;
}

- (void)setDefaultValues {
    _maxHeight = IsiPhone5 ? 504 : 416;
    _cellHeight = 41;
    _separatorHeight = 2;
}

- (void)setSeparatorHeight:(CGFloat)separatorHeight {
    _separatorHeight = (separatorHeight > _cellHeight * 0.5) ? _cellHeight * 0.5 : separatorHeight;
}

- (void)setTableDataArray:(NSArray *)tableDataArray {
//    [_tableDataArray release];
    _tableDataArray = tableDataArray;
    [self updateSize];
    [_table reloadData];
}

- (void)updateSize {
    CGRect rect = self.frame;
    
    CGFloat listHeight = _cellHeight * _tableDataArray.count;
    listHeight = listHeight > _maxHeight ? _maxHeight : listHeight;
    rect.size.height = listHeight;
    self.frame = rect;
    _table.frame = CGRectMake(0, 0, _contentWidth, rect.size.height);
}

- (void)addTableView {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _contentWidth, 0)];
    _table.backgroundColor = COLORWHITE(230); //[UIColor colorWithRed:226.0/255.0 green:226.0/255.0 blue:226.0/255.0 alpha:1.0];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    _table.bounces = NO;
    [self addSubview:_table];
}

- (void)showAndHideButtonList {
    if (_isMoving) {
        return;
    }
    
    if (_isShow == NO) {
        self.hidden = NO;
    }
    _isShow = !_isShow;
    [UIView beginAnimations:@"move" context:nil];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animatedStop:)];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionNone
                           forView:self cache:YES];
    _isMoving = YES;
    if (_isShow) {
        CGRect rect = self.frame;
        rect.origin.x = ScreenWidth - _contentWidth;
        self.frame = rect;
    } else {
        CGRect rect = self.frame;
        rect.origin.x = ScreenWidth;
        self.frame = rect;
    }
    
    [UIView commitAnimations];
    
}

- (void)animatedStop:(id)sender {
    _isMoving = NO;
    if (_isShow == NO) {
        self.hidden = YES;
    }
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_delegate && [_delegate respondsToSelector:@selector(cellClicked:atIndexPath:)]) {
        [_delegate cellClicked:[_tableDataArray objectAtIndex:indexPath.row] atIndexPath:indexPath];
        [self showAndHideButtonList];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.textLabel.text = [_tableDataArray objectAtIndex:indexPath.row];
//    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    
    UILabel *label = [[UILabel alloc] init];
    [label setFont:[UIFont systemFontOfSize:14]];
    label.text = [_tableDataArray objectAtIndex:indexPath.row];
    [label sizeToFit];
    label.backgroundColor = [UIColor clearColor];
    CGRect rect = label.frame;
    rect.origin.x = 12;//45
    label.frame = rect;
    CGPoint center = label.center;
    center.y = cell.center.y;
    label.center = center;
    [cell addSubview:label];
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    cell.backgroundColor = [UIColor clearColor];
    if (_imageDataArray.count>indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:[_imageDataArray objectAtIndex:indexPath.row]];
    }

//    UIImageView *separatorImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, _cellHeight-_separatorHeight, _contentWidth, _separatorHeight)];
    UIImageView *separatorImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, _cellHeight-_separatorHeight, 0, 0)];
    separatorImage.image = [UIImage imageNamed:@"img_rightbutton_popview_divider.png"];
    [cell addSubview:separatorImage];

    UIImageView *selectedImage = [[UIImageView alloc] initWithFrame:cell.bounds];
    selectedImage.image = [UIImage imageNamed:@"img_rightbutton_popview_item_bg_pressed.png"];
    cell.selectedBackgroundView = selectedImage;
    return cell;
}

@end
